import Router from 'koa-router';
import CategoryDB from '../db/category';

const category = new Router();

category.get('/list', async (ctx) => {
  const body = {
    success: true,
    code: 0,
    data: null,
    msg: '',
  };
  try {
    const res = await CategoryDB.query();
    body.data = res;
  } catch (error) {
    body.msg = error.message;
    body.success = false;
  }
  ctx.body = body;
});

category.post('/add', async (ctx) => {
  const data = ctx.request.body;
  const params = {
    name: data.name,
    icon: data.icon,
  };
  const body = {
    success: true,
    code: 0,
    data: null,
    msg: '',
  };
  try {
    const res = await CategoryDB.save(params);
    body.data = res;
  } catch (error) {
    body.msg = error.message;
    body.success = false;
  }

  ctx.body = body;
});

category.get('/add', async (ctx) => {
  const body = {
    success: true,
    code: 0,
    data: null,
    msg: '',
  };
  try {
    const res = await CategoryDB.save({ name: 'test1' });
    body.data = res;
  } catch (error) {
    body.msg = error.message;
    body.success = false;
  }

  ctx.body = body;
});
export default category;
